package org.jeecg.modules.flow.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.flow.aspect.FlowLog;
import org.jeecg.modules.flow.record.entity.WmsFlowRecord;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.entity.dto.WorkFlowInfoDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    流程操作信息
 * 创建人员:    zhaojun
 * 创建时间:    2023-09-14
 */
@Api(tags = "流程操作信息")
@RestController
@RequestMapping("/flow/info")
@Slf4j
@RequiredArgsConstructor
public class FlowInfoContorller {

    private final WorkFlowService workFlowService;

    /**
     * 查询当前业务记录，所处流程节点信息
     * 1.infoId 存在业务id，使用业务id确认当前展示的节点
     * 2.如果未生成业务节点记录信息，则未保存
     * 先使用业务类型和单据类型从字典中展示当前需要展示的节点
     *
     * @param workFlowInfoDto
     * @return
     */
    @ApiOperation(value = "流程操作信息-查询当前记录的流程状态", notes = "流程操作信息-查询当前记录的流程状态")
    @GetMapping("/current-status-info")
    public Result<List<WmsFlowRecord>> workFlowInfo(WorkFlowInfoDto workFlowInfoDto) {
        return Result.ok(workFlowService.workFlowInfo(workFlowInfoDto));
    }

    /**
     * 流程操作信息-提交操作流程信息
     *
     * @param base
     */
    @ApiOperation(value = "流程操作信息-提交操作流程信息", notes = "流程操作信息-提交操作流程信息")
    @PostMapping("/commit")
    @FlowLog(source = "commit")
    public Result commit(@RequestBody @Validated BaseFlowDto base, HttpServletRequest request) {
        base.setRequest(request);
        workFlowService.commit(base);
        return Result.ok(base.getResult());
    }

    /**
     * 流程操作信息-重试操作流程信息
     *
     * @param base
     */
    @ApiOperation(value = "流程操作信息-重试操作流程信息", notes = "流程操作信息-重试操作流程信息")
    @PostMapping("/retry")
    @FlowLog(source = "retry")
    public Result retry(@RequestBody @Validated BaseFlowDto base, HttpServletRequest request) {
        base.setRequest(request);
        workFlowService.retry(base);
        return Result.ok(base.getResult());
    }

    /**
     * 中止操作流程
     *
     * @param base
     */
    @ApiOperation(value = "流程操作信息-中止操作流程", notes = "流程操作信息-中止操作流程")
    @PostMapping("/interrupt")
    @FlowLog(source = "interrupt")
    public Result interrupt(@RequestBody @Validated BaseFlowDto base) {
        workFlowService.interrupt(base);
        return Result.ok(base.getResult());
    }


    /**
     * 查询当前业务记录的流程状态信息
     *
     * @param infoId
     * @return
     */
    @ApiOperation(value = "流程操作信息-查询当前业务记录的流程状态信息", notes = "流程操作信息-查询当前业务记录的流程状态信息")
    @PostMapping("/current-records")
    public Result<List<WmsFlowRecord>> currentBussienssRecords(@RequestParam(value = "infoId") String infoId) {
        return Result.ok(workFlowService.currentBussienssRecords(infoId));
    }

    /**
     * 获取唯一码
     *
     * @return
     */
    @ApiOperation(value = "流程操作信息-获取唯一码", notes = "流程操作信息-获取唯一码")
    @GetMapping("/unique-code")
    public Result<String> getUniqueCode() {
        return Result.ok(UUIDGenerator.generate());
    }

    /**
     * 草稿删除
     *
     * @return
     */
    @ApiOperation(value = "流程操作信息-草稿删除", notes = "流程操作信息-草稿删除")
    @DeleteMapping("/{infoId}")
    public Result delete(@PathVariable(value = "infoId") String infoId) {
        workFlowService.delete(infoId);
        return Result.ok();
    }

    /**
     * 流程操作信息-手动流转流程
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "流程操作信息-手动流转流程", notes = "流程操作信息-手动流转流程")
    @PostMapping("/backAutoCommit")
    @FlowLog(source = "backAutoCommit")
    public Result backAutoCommit(@RequestBody @Validated JSONObject jsonObject) {
        workFlowService.backAutoCommit(jsonObject.getString("infoId"), ObjectUtil.isNotEmpty(jsonObject.get("data")) ? jsonObject.get("data") : null);
        return Result.ok();
    }

    /**
     * 暂停流程
     *
     * @param infoId
     */
    @ApiOperation(value = "流程操作信息-暂停流程", notes = "流程操作信息-暂停流程")
    @PostMapping("/pauseFlow")
    @FlowLog(source = "pauseFlow")
    public Result pauseFlow(String infoId) {
        workFlowService.pauseFlow(infoId,"");
        return Result.ok();
    }
}
